package scenic;

import java.awt.Rectangle;
import java.util.Observable;
import java.util.Observer;

import scenic.geom.Affine2;

/**
 * This class is used to draw an image.
 */
public class SceneImage extends SceneLeaf implements Observer {
	private Image image;
	
	/**
	 * Creates a SceneImage object with the given image.
	 * 
	 * @param image the image.
	 */
	public SceneImage(Image image) {
		setImage(image);
	}
	
	/** 
	 * Gets the image.
	 */
	public Image getImage() {
		return image;
	}

	/**
	 * Sets the image.
	 */
	public void setImage(Image image) {
		if(image != null)
			image.deleteObserver(this);
		this.image = (Image)image;
		if(getVisibility() && image != null)
			image.addObserver(this);
		changed();
	}

	void setVisibility(boolean state) {
		super.setVisibility(state);
		if(image != null) {
			if(state)
				image.addObserver(this);
			else 
				image.deleteObserver(this);
		}
	}
	
	void draw(DrawContext context, Affine2 transform, Rectangle visibleArea) {
		if(image == null || image.getWidth() == 0 || image.getHeight() == 0)
			return;

		context.renderer.color(context.context, new Color(1.0f, 1.0f, 1.0f, 1.0f));
		context.renderer.setTransform(context.context, transform);
		context.renderer.drawImage(context.context, image.getId());
	}

	Rectangle getBounds(Affine2 transform) {
		return Affine2.transform(transform, 
				new Rectangle(0, 0, image.getWidth(), image.getHeight()));
	}

	public void update(Observable arg0, Object arg1) {
		changed(null);
	}	
}
